package hhm.android.library.netWorkState

import android.app.Application
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkRequest
import android.os.Build

class NetworkStateUtils {

    private val STATE_CONNECT = 1
    private val STATE_UN_CONNECT = 2
    private val STATE_UN_KNOWN = 3

    private lateinit var application: Application
    private lateinit var listener: NetworkStateListener
    private var receiver: NetworkReceiver? = null
    private var nowState = STATE_UN_KNOWN

    private constructor()
    private constructor(application: Application, networkStateListener: NetworkStateListener) {
        this.application = application
        listener = networkStateListener
        setNetworkChangeListener()
    }

    companion object {
        @Volatile
        private var instance: NetworkStateUtils? = null
        fun getInstance(
            application: Application,
            networkStateListener: NetworkStateListener
        ): NetworkStateUtils? {
            if (instance == null) {
                synchronized(NetworkStateUtils::class.java) {
                    if (instance == null) {
                        instance = NetworkStateUtils(application, networkStateListener)
                    }
                }
            }
            return instance
        }
    }

    private fun setNetworkChangeListener() {
        val connMgr =
            application.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT !== Build.VERSION_CODES.M) {
            //高版本使用ConnectivityManager.NetworkCallback来监听网络状态变更
            connMgr.requestNetwork(
                NetworkRequest.Builder().build(),
                object : ConnectivityManager.NetworkCallback() {
                    override fun onAvailable(network: Network) {
                        super.onAvailable(network)
                        if (nowState == STATE_CONNECT) {
                            return
                        }
                        nowState = STATE_CONNECT
                        listener.connect()
                    }

                    override fun onLost(network: Network) {
                        super.onLost(network)
                        if (nowState == STATE_UN_CONNECT) {
                            return
                        }
                        nowState = STATE_UN_CONNECT
                        listener.unConnect()
                    }
                })
        } else {
            //低版本需要使用广播来接收
            receiver = NetworkReceiver()
            val intentFilter = IntentFilter()
            intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
            application.registerReceiver(receiver, intentFilter)
        }
    }

    fun destory() {
        if (receiver != null) {
            application.unregisterReceiver(receiver)
        }
    }


    /**
     * 网络状态监听广播
     */
    inner class NetworkReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent?.action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                if (NetworkUtils.isNetworkAvailable(application)) {
                    if (nowState == STATE_CONNECT) {
                        return
                    }
                    nowState = STATE_CONNECT
                    listener.connect()
                } else {
                    if (nowState == STATE_UN_CONNECT) {
                        return
                    }
                    nowState = STATE_UN_CONNECT
                    listener.unConnect()
                }
            }
        }
    }
}